package polymorphic;

import javax.sound.midi.Soundbank;

public class test1 {
    //    public static void func(Animal animal) {
//        animal.eat();
//    }
//    public static Animal func(String name1) {
//        if ("小七".equals(name1)) {
//            return new Dog("小七", 1);
//        }else
//            return null;
//
//    }

    public static void main(String[] args) {
        Dog dog=new Dog("小七", 5);
        Cat cat=new Cat("大胖橘",3);
        Animal animal=cat;

        if(animal instanceof Cat){
            cat=(Cat) animal;
            cat.sleep();
        }
        if(animal instanceof Dog){
            dog=(Dog) animal;
            dog.run();
        }



        //dog=(Dog)animal;
        // func(dog);
//        Animal animal=func("小七");
//        animal.eat();
    }
}

class Animal {
    public String name;
    public int age;

    public Animal(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public void eat() {
        System.out.println(name + "吃东西");
    }
}

class Dog extends Animal {
    public Dog(String name, int age) {
        super(name, age);
    }

    public void eat() {
        System.out.println(name + "在吃狗粮");
    }
    public void run(){
        System.out.println(name+"在奔跑");
    }
}
class Cat extends Animal {
    public Cat(String name, int age) {
        super(name, age);
    }
    public void eat() {
        System.out.println(name + "在吃猫粮");
    }
    public void sleep(){
        System.out.println(name+"在睡觉");
    }
}

